package com.egao.common.module.validate.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.exception.BusinessException;
import com.egao.common.core.exception.ParameterException;
import com.egao.common.core.web.JsonResult;
import com.egao.common.module.validate.entity.ValidateRule;
import com.egao.common.module.validate.entity.ValidateRuleItem;
import com.egao.common.module.validate.mapper.ValidateRuleItemMapper;
import com.egao.common.module.validate.service.ValidateRuleItemService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class ValidateRuleItemServiceImpl extends ServiceImpl<ValidateRuleItemMapper, ValidateRuleItem>
        implements ValidateRuleItemService {

    @Override
    public JsonResult insertValidateRuleItem(ValidateRuleItem validateRuleItem) {
        verifyParam(validateRuleItem);
        if (!save(validateRuleItem)) {
            return JsonResult.error("验证规则添加失败");
        }
        return JsonResult.ok("验证规则添加成功");
    }

    @Override
    public JsonResult updateValidateRuleItem(ValidateRuleItem validateRuleItem) {
        verifyParam(validateRuleItem);
        if (!updateById(validateRuleItem)) {
            return JsonResult.error("验证规则修改失败");
        }
        return JsonResult.ok("验证规则修改成功");
    }

    @Override
    public JsonResult deleteValidateRuleItem(Integer id) {
        if (id == null) {
            throw new ParameterException("参数对象为空");
        }
        if (!removeById(id)) {
            return JsonResult.error("验证规则删除失败");
        }
        return JsonResult.ok("验证规则删除成功");
    }

    /**
     * 验证参数
     * @param validateRuleItem
     * @return boolean
     */
    private void verifyParam(ValidateRuleItem validateRuleItem) {
        if (Objects.isNull(validateRuleItem)) {
            throw new ParameterException("参数对象不能为空");
        }
        if (Objects.isNull(validateRuleItem.getValidateRuleId())) {
            throw new ParameterException("验证规则ID不能为空");
        }
        if (Objects.isNull(validateRuleItem.getModule()) || StrUtil.isBlank(validateRuleItem.getModule())) {
            throw new ParameterException("验证模块不能为空");
        }
        if (Objects.isNull(validateRuleItem.getCategory())) {
            throw new ParameterException("规则项类型不能为空");
        }
        if (Objects.isNull(validateRuleItem.getFieldTitle()) || StrUtil.isBlank(validateRuleItem.getFieldTitle())) {
            throw new ParameterException("字段描述不能为空");
        }
        if (Objects.isNull(validateRuleItem.getFieldName()) || StrUtil.isBlank(validateRuleItem.getFieldName())) {
            throw new ParameterException("字段标识不能为空");
        }
        if (Objects.isNull(validateRuleItem.getFieldValue()) || StrUtil.isBlank(validateRuleItem.getFieldValue())) {
            throw new ParameterException("规则值不能为空");
        }
        if (Objects.isNull(validateRuleItem.getRule())) {
            throw new ParameterException("规则不能为空");
        }
    }
}
